//
//  TushangCollectionViewCell.m
//  cityCar
//
//  Created by 常宽 on 14/10/31.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TushangCollectionViewCell.h"
#import "Connection.h"
#import "TuShangTableViewCell.h"

@interface TushangCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)LoadingView *load;

@end

@implementation TushangCollectionViewCell

- (void)dealloc
{
    [_tableView release];
    [_array release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.array = [NSMutableArray array];
        
        [self netWork];
        
    }
    return self;
}

- (void)netWork
{
    

    
    NSString *str = @"http://agent1.pconline.com.cn:8941/pcautophoto/iphone_cate_json.jsp?id=89";
    [Connection networkHandlerWith:str block:^(id result) {
        self.array = [result objectForKey:@"groups"];
        [_tableView reloadData];
        [_load removeFromSuperview];
    }];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self setupSubview];
}

- (void)setupSubview
{
    self.tableView = [[UITableView alloc]initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
    [self.contentView addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    [_tableView release];
    
    self.load= [[LoadingView alloc]initWithFrame:CGRectMake(0, 30, self.contentView.frame.size.width, self.contentView.frame.size.height - 64)];
    [self.contentView addSubview:_load];
    [self.contentView bringSubviewToFront:_load];
    [_load release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TuShangTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CK_tushangTV"];
    if (!cell) {
        cell = [[TuShangTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CK_tushangTV"];
    }
    cell.tuShangDic = [self.array objectAtIndex:indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.contentView.frame.size.height / 2.5;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *url = [[self.array objectAtIndex:indexPath.row] objectForKey:@"url"];
    NSString *count = [[self.array objectAtIndex:indexPath.row] objectForKey:@"photoCount"];
    self.block(url, count);
}

@end
